<script setup lang="ts">
import { showSuccessToast, showToast } from 'vant'
import { ref } from 'vue'
import { codeRules, mobileRules, passwordRules } from '@/utils/rules'
import { loginByMobile, loginByPassword } from '@/services/user'
import { useUserStore } from '@/stores'
import { useRoute, useRouter } from 'vue-router'
import { useSendMobileCode } from '@/composable'

const mobile = ref('13230000081')
const password = ref('abc12345')
const code = ref('')
const agree = ref(true)

const useStore = useUserStore()
const router = useRouter()
const route = useRoute()
const onSubmit = async () => {
  // console.log(route)

  if (!agree.value) return showToast('请勾选我已同意')
  // console.log('submit', mobile.value, password.value)
  const { data } = isPass.value
    ? await loginByPassword(mobile.value, password.value)
    : await loginByMobile(mobile.value, code.value)
  showSuccessToast('登录成功')
  useStore.setUser(data)
  const returnUrl = route.query.returnUrl as string
  router.replace(returnUrl || '/user')
}
const isPass = ref(true)

const { send, formRef, time } = useSendMobileCode(mobile)
const show = ref(false)

// defineOptions({
//   name: 'Login'
// })
</script>

<template>
  <div class="login-page">
    <cp-nav-bar
      right-text="注册"
      @click-right="$router.push('/register')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>{{ isPass ? '短信验证码登录' : '密码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" @submit="onSubmit" ref="formRef">
      <van-field
        placeholder="请输入手机号"
        v-model="mobile"
        name="mobile"
        type="tel"
        :rules="mobileRules"
      ></van-field>
      <van-field
        placeholder="请输入密码"
        v-model="password"
        :type="`${show ? 'text' : 'password'}`"
        name="password"
        :rules="passwordRules"
        v-if="isPass"
        ><template #button>
          <cp-icon
            :name="`login-eye-${show ? 'on' : 'off'}`"
            @click="show = !show"
          ></cp-icon>
        </template>
      </van-field>
      <van-field
        placeholder="请输入验证码"
        name="code"
        v-model="code"
        :rules="codeRules"
        v-else
        ><template #button>
          <span class="btn-send" :class="{ active: time > 0 }" @click="send">{{
            time > 0 ? `${time}s后再次发送` : '发送验证码'
          }}</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <a
          @click="useStore.setReturnUrl($route.query.returnUrl as string)"
          href="https://graph.qq.com/oauth2.0/authorize?client_id=102015968&response_type=token&scope=all&redirect_uri=http%3A%2F%2Fconsult-patients.itheima.net%2Flogin%2Fcallback"
        >
          <img src="@/assets/qq.svg" alt="" />
        </a>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import '@/styles/login.scss';
</style>
